﻿using System;
using System.IO;
using System.Windows.Forms;
using CloudShot.Core.Interfaces;
using CloudShot.Core.Utils;
using CloudShot.ImageStorages;
using CloudShot.Properties;

namespace CloudShot.Controls
{
  //TODO REFACTOR
  public partial class FileSystemStorageSettingsControl : UserControl, ISettingsControl
  {
    private readonly FileSystemStorage _storage;

    public IImageStorage Storage
    {
      get { return _storage; }
    }

    public Control Control
    {
      get { return this; }
    }

    public IConfigurationSection Configuration { get; set; }

    public FileSystemStorageSettingsControl(FileSystemStorage storage)
    {
      _storage = storage;

      InitializeComponent();

      lblInfo.Text = Resources.FileSystemStorageSettingsControl_PluginGeneralDescription;
    }

    public void Loading()
    {
      cboImageFormat.Items.Clear();

      foreach (string extension in _storage.SupportedFormats.GetImageExtensions())
      {
        cboImageFormat.Items.Add(extension);
      }

      var settings = Configuration.GetOrCreateAs<FileSystemSettings>();

      cboImageFormat.SelectedItem = settings.CurrentImageFormat.GetImageExtension();
      txtPath.Text = settings.FolderToSave;
      txtPath.SelectionStart = txtPath.Text.Length;
      toolTip.SetToolTip(txtPath, txtPath.Text);

      chkClickableLinks.Checked = settings.IsClickableLinksEnabled;
    }

    public bool ValidateData()
    {
      //TODO по идее надо делать такую же проверку при шоте
      try
      {
        Directory.CreateDirectory(txtPath.Text); //TODO плохо создавать папку в методе Validate!!!
      }
      catch (Exception err)
      {
        string message = string.Format(Resources.FileSystemStorageSettingsControl_InvalidFolderPath, err.Message);
        MessageBox.Show(message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        txtPath.Focus();
        return false;
      }

      return true;
    }

    public void OkClicked()
    {
      var settings = Configuration.GetOrCreateAs<FileSystemSettings>();

      settings.CurrentImageFormat = ImageTypeUtils.GetImageType((string)cboImageFormat.SelectedItem);
      settings.FolderToSave = txtPath.Text;
      settings.IsClickableLinksEnabled = chkClickableLinks.Checked;

      Configuration.SaveAs(settings);
    }

    public void CancelClicked() { }

    private void OnBrowseClick(object sender, EventArgs e)
    {
      using (var dialog = new FolderBrowserDialog())
      {
        dialog.Description = Resources.FileSystemStorageSettingsControl_SelectFolderDialogTitle;

        if (!string.IsNullOrEmpty(txtPath.Text))
          dialog.SelectedPath = txtPath.Text;

        if (dialog.ShowDialog() == DialogResult.OK)
        {
          txtPath.Text = dialog.SelectedPath;
          txtPath.SelectionStart = txtPath.Text.Length;
          toolTip.SetToolTip(txtPath, txtPath.Text);
        }
      }
    }
  }
}